<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/28
 * Time: 13:52
 */
namespace common\sources\write\taskorder;

use Yii;
use common\models\craftsman\TaskOrder;

class TaskOrderAccess implements ITaskOrderAccess
{
    public function ModifyOrderPrice($data) {
        $order = TaskOrder::findOne(['id'=>$data['id']]);
        if ($order) {
            $order->actual_price = $data['price'];
            $order->refund_amount = $data['price'];
            $order->advance_amount = $data['price'];
            $order->actual_refunds = $data['price'];
            if ($order->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyOrderNo($data) {
        $order = TaskOrder::findOne(['id'=>$data['id']]);
        if ($order) {
            $order->pay_order_num = $data['orderNo'];
            if ($order->save()) {
                return true;
            }
        }
        return false;
    }

    public function PassOrder($data) {
        $order = TaskOrder::findOne(['id'=>$data['id']]);
        if ($order) {
            $order->order_status = 3;
            $order->audit_operator = $data['audit_operator'];
            $order->audit_time = time();
            if ($order->save()) {
                return true;
            }
        }
        return false;
    }

    public function EditOrder($data) {
        $order = TaskOrder::findOne(['id'=>$data['id']]);
        if ($order) {
            $order->actual_price = $data['actual_price'];
            $order->pro_name = $data['pro_name'];
            $order->pro_code = $data['pro_code'];
            $order->city_name = $data['city_name'];
            $order->city_code = $data['city_code'];
            $order->area_name = $data['area_name'];
            $order->area_code = $data['area_code'];
            $order->addr = $data['addr'];
            $order->receive_name = $data['receive_name'];
            $order->receive_phone = $data['receive_phone'];
            $order->pay_url = $data['pay_url'];
            $order->addr_url = $data['addr_url'];
            if ($order->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyOrderMark($data) {
        $order = TaskOrder::findOne(['id'=>$data['id']]);
        if ($order) {
            $order->order_mark = $data['mark_id'];
            $order->order_remark = $data['reason'];
            if ($order->save()) {
                return true;
            }
        }
        return false;
    }
    public function deliverGoods($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,merchant_time = :merchant_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':merchant_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    public function receivingGood($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,favorable_time = :favorable_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':favorable_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    public function revokeReceive($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,favorable_time = :favorable_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':favorable_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }

    public function refunds($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,actual_price = :actual_price,advance_amount = :advance_amount,actual_refunds = :actual_refunds,refund_amount = :refund_amount,plat_return_time = :plat_return_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':actual_price' => $data['actual_price'],
                ':actual_refunds' => $data['actual_refunds'],
                ':refund_amount' => $data['refund_amount'],
                ':advance_amount' => $data['advance_amount'],
                ':plat_return_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    //买手确认返款
    public function confirmRefunds($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,confirm_time = :confirm_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':confirm_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    //撤销返款
    public function revokeRefunds($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,favorable_time = :favorable_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':favorable_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    //撤销买手驳回
    public function cancelReject($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,plat_return_time = :plat_return_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':plat_return_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }
    //打印快递单号
    public function printTaskOrderByNum($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,express_num = :express_num,print_time = :print_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':express_num' => $data['express_num'],
                ':print_time' => time(),
                ':update_time' => time(),
                ':id' => $data['task_order_id']
            ])->execute();
    }

    public function TaskOrderCancelStatus($data)
    {
        return TaskOrder::updateAll(['order_status' => $data['order_status']],['id' => $data['task_order_ids']]);
    }

	/**
	 * modifyTask  取消任务使用
	 * @param $data
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/9
	 * @return int
	 * @throws \yii\db\Exception
	 */
	public function modifyTask($data) {
		$sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,give_up_time = :give_up_time,update_time = :update_time WHERE id = :id ';
		return Yii::$app->db->createCommand($sql)
			->bindValues([
				':order_status' => $data['order_status'],
				':give_up_time' => time(),
				':update_time'  => time(),
				':id'           => $data['id']
			])->execute();
	}
}